package userInterface;
import dataProces.DatabaseOperations;

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;

public class Logout extends JDialog implements ActionListener {
    JPanel[] jPanels = new JPanel[3];
    JButton[] jButtons = new JButton[2];
    String[] prompt = { "输入密码", "注销", "返回", "注销界面" };
    JTextField jField = new JTextField(15);

    public Logout() {
        Container container = this.getContentPane();
        container.setLayout(new GridLayout(3, 1));
        for (int i = 0; i < jPanels.length; i++) {
            jPanels[i] = new JPanel();
            if (i == 0) {
                jPanels[i].add(new JLabel(prompt[i]));
                jPanels[i].add(jField);
            } else {
                jButtons[i - 1] = new JButton(prompt[i]);
                jButtons[i - 1].addActionListener(this);
                jPanels[i].add(jButtons[i - 1]);
            }
            container.add(jPanels[i]);
        }
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setSize(500, 300);
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation((screen.width - 500) / 2, (screen.height - 300) / 2);
        setTitle(prompt[3]);
        setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        DatabaseOperations dOperations = new DatabaseOperations();
        if (e.getSource() == jButtons[0]) {
            String password=jField.getText().trim();
            if(password.equals(dOperations.dataQueries("user", Login.user, "password"))){
                dOperations.dataDelete(Login.user);
                JOptionPane.showMessageDialog(null,"已注销！");
                dispose();
                new Login();
            }else JOptionPane.showMessageDialog(null,"密码错误！");
        } else if (e.getSource() == jButtons[1]) {
            dispose();
            new UserMenu();
        }
    }

    public static void main(String[] args) {
        new Logout();
    }
}
